package com.qipay.sp.excutor.swiftpass;

import com.qipay.baas.model.Order;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.SignSortMD5Utils;
import com.qipay.utils.XmlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class SwiftpassWxWapPayExcutor extends AbstractExcutor {

    private final static String KEY_NAME="key";
    private final static String CONFIG_KEY_NAME="key";
    private final static String CONFIG_MCHID_NAME="mch_id";
    private final static String CONFIG_IP_NAME="ip";
//    private final static String CONFIG_SUB_APPID="sub_appid";
    private final static String SIGN_NAME="sign";
    private final static String API_URL="https://pay.swiftpass.cn/pay/gateway";

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.WX_WAP_PAY;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("out_trade_no", order.getTradeNo());
        apiParams.put("body", order.getBody());
        apiParams.put("total_fee", order.getAmount());
        apiParams.put("nonce_str", new Date().getTime());

        apiParams.putAll(extendOrderParams);
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {

        Object key = getVal(spconfig, SwiftpassWxWapPayExcutor.CONFIG_KEY_NAME);
        Object mchId = getVal(spconfig, SwiftpassWxWapPayExcutor.CONFIG_MCHID_NAME);
        Object ip = getVal(spconfig, SwiftpassWxWapPayExcutor.CONFIG_IP_NAME);
//        Object subAppid = getVal(spconfig, SwiftpassWxWapPayExcutor.CONFIG_SUB_APPID);

        params.put("mch_id", mchId);
        params.put("mch_create_ip", ip);
//        params.put("sub_appid", subAppid);

        String sign = SignSortMD5Utils.getSign(params,
                key.toString(),
                SwiftpassWxWapPayExcutor.SIGN_NAME,
                SwiftpassWxWapPayExcutor.KEY_NAME);

        params.put(SwiftpassWxWapPayExcutor.SIGN_NAME, sign);

        String result = sendPost(params, SwiftpassWxWapPayExcutor.API_URL);
        if(StringUtils.isBlank(result)){
            throw new Exception("请求swiftpass的微信WAP接口失败");
        }
        Map<String, Object> responseMap = XmlUtils.toMap(result);
        Map<String, Object> resultMap = new HashMap<>();

        if(responseMap == null || !responseMap.containsKey("status") ||
                responseMap.get("status") == null || !responseMap.get("status").toString().equals("0"))
        {
            logger.error("请求swiftpass的微信WAP接口失败:" + result);
            return null;
        }
        if(!isResponseValid(responseMap, key.toString(), SwiftpassWxWapPayExcutor.SIGN_NAME,
                SwiftpassWxWapPayExcutor.KEY_NAME)) {
            logger.error("请求swiftpass的微信WAP返回值的签名失败:" + result);
            return null;
        }

        resultMap.put("result_code", responseMap.get("result_code"));

        if(!responseMap.get("result_code").toString().equals("0"))
        {
            logger.error("请求swiftpass的微信WAP接口业务执行失败:" + result);
            resultMap.put("err_code", responseMap.get("err_code"));
            resultMap.put("err_msg", responseMap.get("err_msg"));
            return resultMap;
        }

        resultMap.put("pay_info", responseMap.get("pay_info"));
        return resultMap;
    }
}
